package com.spider.moudle.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Linbo Hu on 2017/11/21.
 * 地域实体
 */
public class Area {

    private static JSONObject object = null;

    private static Map<String, String> map = null;

    static {
        InputStream inputStream = null;
        try {
            inputStream = Area.class.getResourceAsStream("/META-INF/area.json");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line.trim());
            }
            object = JSON.parseObject(sb.toString());

            map = new HashMap<>();
            JSONArray jsonArray=JSONArray.parseArray(object.get("data").toString());
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject= JSONObject.parseObject(jsonArray.get(i).toString());
                String provinceName=jsonObject.get("provinceName").toString();
                if(jsonObject.get("city")!=null) {
                    JSONArray cityArray = JSONArray.parseArray(jsonObject.get("city").toString());
                    for (int j = 0; j < cityArray.size(); j++) {
                        JSONObject cityObject = JSONObject.parseObject(cityArray.get(j).toString());
                        String cityName = cityObject.get("cityName").toString();
                        map.put(cityName, provinceName);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private String country;

    private String province;

    private String city;

    private String lat;

    private String lon;


    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public Area() {
    }

    public Area(String province) {
        this.province = province;
    }

    public Area(String province, String city) {
        this.province = province;
        this.city = city;
    }

    public Area(String country, String province, String city) {
        this.country = country;
        this.province = province;
        this.city = city;
    }

    public Area(String province, String city, String lat, String lon) {
        this.province = province;
        this.city = city;
        this.lat = lat;
        this.lon = lon;
    }

    /**
     * 从库中获取某个城市
     *
     * @param key
     * @return
     */
    public static Area search(String key) {
        Area area = null;
        try {
            //省
            List<String> provinceList = (List<String>) JSONPath.eval(object, "$.data[provinceName like '%" + key + "%'] ");
            //市
            List<String> cityList = (List<String>) JSONPath.eval(object, "$.data.city[cityName like '%" + key + "%'] ");
            if (provinceList.size() > 0) {
                String province = provinceList.toString();
                JSONArray provinceArray = JSONObject.parseArray(province);
                JSONObject provinceObject = provinceArray.getJSONObject(0);
                provinceObject.remove("city");
                String lon = provinceObject.get("lon").toString();
                String lat = provinceObject.get("lat").toString();
                String provinceName = provinceObject.get("provinceName").toString();
                area = new Area(provinceName);
                area.setCountry("中国");
                area.setLat(lat);
                area.setLon(lon);
            } else if (cityList.size() > 0) {
                String city = cityList.toString();
                JSONArray cityArray = JSONObject.parseArray(city);
                JSONObject cityObject = cityArray.getJSONObject(0);
                String lon = cityObject.get("lon").toString();
                String lat = cityObject.get("lat").toString();
                String cityName = cityObject.get("cityName").toString();
                String provinceName = map.get(cityName);

                if(!cityName.endsWith("市")){
                    cityName=cityName+"市";
                }
                area = new Area(provinceName, cityName, lat, lon);
                area.setCountry("中国");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return area;
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static void main(String[] args) {
        Area ss = search("烟台");
        System.out.println(ss);
    }
}
